import Request from '../util/Request'
import md5 from 'js-md5';
import AccountConfig from "../config/AccountConfig";
import App from '../config/App';

const create = (param, callback) => {
    let url = App.HOST + '/user/stateflow/createStateFlow.json';
    param = {
        ...param,
        sign: md5(AccountConfig.COMPOSE + param.accessKey),
        apply: AccountConfig.APPLY_NAME
    };

    let req = Request.getRequest(param, url);
    Request.post(req, callback);
};

const modify = (param, callback, fail) => {
    let url = App.HOST + '/user/stateflow/modifyStateFlow.json';
    param = {
        ...param,
        sign: md5(AccountConfig.COMPOSE + param.accessKey),
        apply: AccountConfig.APPLY_NAME
    };

    let req = Request.getRequest(param, url);
    Request.post(req, callback, fail);
};

const remove = (param, callback, fail) => {
    let url = App.HOST + '/user/stateflow/removeStateFlow.json';
    param = {
        ...param,
        sign: md5(AccountConfig.COMPOSE + param.accessKey + param.id),
        apply: AccountConfig.APPLY_NAME
    };

    let req = Request.getRequest(param, url);
    Request.post(req, callback, fail);
};

const query = (param, callback, fail) => {
    let url = App.HOST + '/user/stateflow/queryStateFlow.json';
    param = {
        ...param,
        sign: md5(AccountConfig.COMPOSE + param.accessKey),
        apply: AccountConfig.APPLY_NAME
    };

    let req = Request.getRequest(param, url);
    Request.post(req, callback, fail);
};

export default {
    create: create,
    modify: modify,
    remove: remove,
    query: query
}
